﻿
using System; 

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class AbsTaskComp : GkTransaction
    {
        public static LocSelection selection;

        public TaskResDto createTask(  int from, int des, string relyId = "") {

            CoreTask task = new CoreTask(); 
            task.Status = (int)getStatus(0);
            task.bussType = taskBussType(0);
            task.waveNo = getWaveNO(0); 
            task.info = createCoreTaskInfo(task, from, des);
            insert(task); 
            long taskId = task.id;
        
            long paramId = createParam(  taskId, from, des, relyId);
            return new TaskResDto() { taskId = taskId, paramId = paramId };
        }


        public TaskResDto createTask(  AbsCoreStock cs, int des, string relay = "") { 
            CoreTask task = new CoreTask();
            task.stockId = cs.id;
            task.boxCode = cs.boxCode;
            task.Status = (int)getStatus(cs.id);
            task.bussType = taskBussType(cs.id);
            task.waveNo = getWaveNO(cs.id);
            task.diff = cs.deepDiff;
            task.info = createCoreTaskInfo(task, cs.locId, des);
            insert(task);
            long taskId= task.id;   
            long paramTaskId = createParam(  taskId, cs.locId, des, relay);
            return new TaskResDto() { taskId = taskId, paramId = paramTaskId };
        }
        
        public virtual int taskBussType(long stockId) {
            return 0;
        }
        virtual protected  String getWaveNO(long stockId) {
            return "";
        }
        virtual protected string createCoreTaskInfo(CoreTask task,int src,int des) {
            return string.Format("从{0}到{1}", stnToCrane(src), stnToCrane(des));
        }
        protected virtual CoreTaskStatus getStatus(long stockId) {
            return CoreTaskStatus.执行中;
        }

    
        protected virtual string stnToCrane(long loc) {
            if (I18nConfig.isLoc(loc)) {
                return (loc + 10000) / 20000 + "号垛机" + loc;
            } else {
                return loc.ToString();
            }
        }
       
        public long createParam(long taskId, int src, int des, string relyId="", String cName="", int cNo=0,int cdes=0 ) {
            //任务明细添加
            CoreTaskParam param = new CoreTaskParam(); 
            param.taskId = taskId; //task.id 
            param.src = src; 
            param.des = des;
            param.status = newParamStatus(taskId);//新建状态
           
            param.relyId = relyId;
            param.taskNo = TaskNoUtil.getTaskNo(this);
            param.cName = cName;
            param.cNo = cNo;
            param.cStn = src;
            param.cDes = cdes;
            param.type = (byte)getType(src);
            paramInfo(param);
            insert(param);
            return param.id;
        }

        protected virtual TaskParamType getType(int src )
        {
            return  TaskParamType.托盘搬运;
        }

        protected virtual int newParamStatus(long taskId) {
            return (int) CoreTaskParamStatus.可以接收任务;
        }
        protected virtual void paramInfo(CoreTaskParam param) {
            param.param= "";
        } 

       
    }
}

